package org.lw.ssm.mapper;

import org.apache.ibatis.annotations.*;
import org.lw.ssm.Entity.Employee;

import java.util.List;

@Mapper
public interface EmployeeMapper {

    // 根据ID查询员工
    @Select("SELECT * FROM employee WHERE id = #{id}")
    @Results(id = "employeeResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "departmentId", column = "department_id"),
            @Result(property = "department", column = "department_id",
                    one = @One(select = "org.lw.ssm.mapper.DepartmentMapper.getDepartmentById")) // 引用部门
    })
    Employee getEmployeeById(Long id);

    // 查询所有员工
    @Select("SELECT * FROM employee")
    List<Employee> getAllEmployees();

    // 根据部门ID查询员工


    @Select("SELECT * FROM employee WHERE department_id = #{departmentId}")
    @ResultMap("employeeResultMap")
    List<Employee> getEmployeesByDepartmentId(Long departmentId);

    // 插入新员工
    @Insert("INSERT INTO employee(name, department_id) VALUES(#{name}, #{departmentId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertEmployee(Employee employee);

    // 更新员工信息
    @Update("UPDATE employee SET name = #{name}, department_id = #{departmentId} WHERE id = #{id}")
    void updateEmployee(Employee employee);

    // 删除员工
    @Delete("DELETE FROM employee WHERE id = #{id}")
    void deleteEmployee(Long id);
}

